package com.odais.AE_Planner;

import org.andengine.engine.Engine;
import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.entity.scene.Scene;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.util.GLState;

import android.opengl.GLES20;
import android.util.Log;

/**
 * (c) 2010 Nicolas Gramlich
 * (c) 2011 Zynga Inc.
 *
 * @author Nicolas Gramlich
 * @since 22:28:34 - 27.03.2010
 */
public class DoubleSceneSplitScreenEngineOdais extends Engine {
	// ===========================================================
	// Constants
	// ===========================================================

	// ===========================================================
	// Fields
	// ===========================================================

	private Scene mSecondScene;
	private final Camera mSecondCamera;
	private int mLeftMenuSize;

	// ===========================================================
	// Constructors
	// ===========================================================

	public DoubleSceneSplitScreenEngineOdais(final EngineOptions pEngineOptions, final Camera pSecondCamera, int mLeftMenuSize) {
		super(pEngineOptions);
		this.mSecondCamera = pSecondCamera;
		this.mLeftMenuSize = mLeftMenuSize;
	}

	// ===========================================================
	// Getter & Setter
	// ===========================================================

	@Deprecated
	@Override
	public Camera getCamera() {
		return super.mCamera;
	}

	public Camera getFirstCamera() {
		return super.mCamera;
	}

	public Camera getSecondCamera() {
		return this.mSecondCamera;
	}

	@Deprecated
	@Override
	public Scene getScene() {
		return super.getScene();
	}

	public Scene getFirstScene() {
		return super.getScene();
	}

	public Scene getSecondScene() {
		return this.mSecondScene;
	}

	@Deprecated
	@Override
	public void setScene(final Scene pScene) {
		this.setFirstScene(pScene);
	}

	public void setFirstScene(final Scene pScene) {
		super.setScene(pScene);
	}

	public void setSecondScene(final Scene pScene) {
		this.mSecondScene = pScene;
	}

	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================

	@Override
	protected void onUpdateScene(final float pSecondsElapsed) {
		super.onUpdateScene(pSecondsElapsed);

		if(this.mSecondScene != null) {
			this.mSecondScene.onUpdate(pSecondsElapsed);
		}

		this.getSecondCamera().onUpdate(pSecondsElapsed);
	}

	@Override
	protected void onDrawScene(final GLState pGLState, final Camera pFirstCamera) {
		final Camera secondCamera = this.getSecondCamera();

		final int surfaceWidthLeft = this.mLeftMenuSize;
		final int surfaceWidthRight = this.mSurfaceWidth - this.mLeftMenuSize;

		final int surfaceHeight = this.mSurfaceHeight;
		
		//Log.v("Odais", "mSurfaceWidth=" + this.mSurfaceWidth + " surfaceHeight="+this.mSurfaceHeight);

		pGLState.enableScissorTest();

		/* First Screen. With first camera, on the left half of the screens width. */
		if(super.mScene != null) {
			GLES20.glScissor(0, 0, surfaceWidthLeft, surfaceHeight);
			GLES20.glViewport(0, 0, surfaceWidthLeft, surfaceHeight);

			super.mScene.onDraw(pGLState, pFirstCamera);
			pFirstCamera.onDrawHUD(pGLState);
		}

		/* Second Screen. With second camera, on the right half of the screens width. */
		if(this.mSecondScene != null) {
			GLES20.glScissor(surfaceWidthLeft, 0, surfaceWidthRight, surfaceHeight);
			GLES20.glViewport(surfaceWidthLeft, 0, surfaceWidthRight, surfaceHeight);

			this.mSecondScene.onDraw(pGLState, secondCamera);
			secondCamera.onDrawHUD(pGLState);
		}

		pGLState.disableScissorTest();
	}

	@Override
	protected Camera getCameraFromSurfaceTouchEvent(final TouchEvent pTouchEvent) {
		final int surfaceWidthLeft = this.mLeftMenuSize;
		if(pTouchEvent.getX() <= surfaceWidthLeft) {
			return this.getFirstCamera();
		} else {
			return this.getSecondCamera();
		}
	}

	@Override
	protected Scene getSceneFromSurfaceTouchEvent(final TouchEvent pTouchEvent) {
		final int surfaceWidthLeft = this.mLeftMenuSize;
		if(pTouchEvent.getX() <= surfaceWidthLeft) {
			return this.getFirstScene();
		} else {
			return this.getSecondScene();
		}
	}

	@Override
	protected void convertSurfaceTouchEventToSceneTouchEvent(final Camera pCamera, final TouchEvent pSurfaceTouchEvent) {
		final int surfaceWidthLeft = this.mLeftMenuSize;
		final int surfaceWidthRight = this.mSurfaceWidth - this.mLeftMenuSize;

		if(pCamera == this.getFirstCamera()) {
			pCamera.convertSurfaceTouchEventToSceneTouchEvent(pSurfaceTouchEvent, surfaceWidthLeft, this.mSurfaceHeight);
		} else {
			pSurfaceTouchEvent.offset(-surfaceWidthLeft, 0);
			pCamera.convertSurfaceTouchEventToSceneTouchEvent(pSurfaceTouchEvent, surfaceWidthRight, this.mSurfaceHeight);
		}
	}


       @Override
    protected void onUpdateUpdateHandlers(final float pSecondsElapsed) {
        super.onUpdateUpdateHandlers(pSecondsElapsed);
        this.getSecondCamera().onUpdate(pSecondsElapsed);
	}

	@Override
	protected void onUpdateCameraSurface() {
		final int surfaceWidthLeft = this.mLeftMenuSize;
		final int surfaceWidthRight = this.mSurfaceWidth - this.mLeftMenuSize;
        this.getFirstCamera().setSurfaceSize(0, 0, surfaceWidthLeft, this.mSurfaceHeight);
        this.getSecondCamera().setSurfaceSize(0, surfaceWidthLeft, surfaceWidthRight, this.mSurfaceHeight);
	}

	// ===========================================================
	// Methods
	// ===========================================================

	// ===========================================================
	// Inner and Anonymous Classes
	// ===========================================================
}
